import { _decorator, Component, Node, director, v3, SpriteFrame, Sprite, Prefab, instantiate, Button, } from 'cc';
import { EventManager } from '../event/EventManager';

const { ccclass, property } = _decorator;

import { UIManager } from '../common/UIManager';

import { PrefabEnum } from '../common/PrefabEnum';
import { VillageController } from '../village/VillageController';
import { BuildingList } from '../village/BuildingList';
import { UserInfo } from './UserInfo';
import { buildingType } from '../settings/const';
import { BManager } from '../entity/BManager';
import ResManager from './ResManager';


@ccclass('BtnModule')
export class BtnModule extends Component {

    @property(Node)
    AIAssistant: Node;
    @property(Node)
    menu: Node;
    @property(Node)
    build: Node;
    @property(Node)
    train: Node;
  
    @property(Node)
    villageResource: Node;
   
    @property(Node)
    war: Node
    @property(Node)
    shop: Node
    @property(Node)
    package: Node
    @property(Node)
    copy: Node
    @property(Node)
    rank: Node

    @property(Node)
    quit: Node
    @property(Node)
    art: Node
    @property(Node)
    report: Node
    @property(Node)
    heroFight: Node
    @property(Node)
    palace: Node
    @property(Node)
    army: Node
    @property(Node)
    trea: Node

    @property(Node)
    union: Node
    @property(Node)
    develop: Node
    @property(Node)
    transport: Node
    @property(Node)
    bg: Node
    @property(Node)
    rightLayout: Node
    @property(Node)
    menuLayout: Node

    @property(SpriteFrame)
    menuImg1: SpriteFrame
    @property(SpriteFrame)
    menuImg2: SpriteFrame

    start() {
        EventManager.instance.on(EventManager.EventType.BGCLICK, this.onBgClick, this)
        // this.AIAssistant.on(Node.EventType.TOUCH_END, this.onAi, this)
        this.menu.on(Node.EventType.TOUCH_END, this.onMenu, this)
        this.build.on(Node.EventType.TOUCH_END, this.onBuild, this)
        this.train.on(Node.EventType.TOUCH_END, this.onTrain, this)
      
        this.villageResource.on(Node.EventType.TOUCH_END, this.onVill, this)
       

        this.AIAssistant.on(Node.EventType.TOUCH_END, this.onAi, this)
        this.war.on(Node.EventType.TOUCH_END, this.onWar, this)
        this.rank.on(Node.EventType.TOUCH_END, this.onRank, this)
        this.copy.on(Node.EventType.TOUCH_END, this.onCopy, this)
        this.shop.on(Node.EventType.TOUCH_END, this.onShop, this)
        this.package.on(Node.EventType.TOUCH_END, this.onPagekage, this)

        this.quit.on(Node.EventType.TOUCH_END, this.onQuit, this)
        this.art.on(Node.EventType.TOUCH_END, this.onArt, this)
        this.heroFight.on(Node.EventType.TOUCH_END, this.onHeroFight, this)
        this.develop.on(Node.EventType.TOUCH_END, this.onDevelop, this)
        this.transport.on(Node.EventType.TOUCH_END, this.onTransport, this)
        this.army.on(Node.EventType.TOUCH_END, this.onArmy, this)
        this.palace.on(Node.EventType.TOUCH_END, this.onPalace, this)
        this.report.on(Node.EventType.TOUCH_END, this.onReport, this)
        this.union.on(Node.EventType.TOUCH_END, this.onUnion, this)
        this.trea.on(Node.EventType.TOUCH_END, this.onTrea, this)


        this.bg.on(Node.EventType.TOUCH_END, this.onMenu, this)

        this.bg.active = false
        this.menuLayout.active = false

        let name = director.getScene().name
        if (name == "world") {
            this.rightLayout.active = false
        }


        this.lock()
        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.setBtnEnable, this)
    }

    async lock() {
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.BtnLock)
        let btnLock = instantiate(prefab)
        this.heroFight.addChild(btnLock)
    }

    /**设置按钮是否禁用 */
    async setBtnEnable() {
        console.log('set btn enable...')
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.BtnLock)

        let heroMasion = BManager.getBManageById(buildingType.B37_HeroMasion)
        let residence = BManager.getBManageById(buildingType.B25_Residence)
        let palace = BManager.getBManageById(buildingType.B26_Palace)
        let embassy = BManager.getBManageById(buildingType.B18_Embassy)
        let academy = BManager.getBManageById(buildingType.B22_Academy)
        let market = BManager.getBManageById(buildingType.B17_Market)
        let barracks = BManager.getBManageById(buildingType.B19_Barracks)
        let treasury = BManager.getBManageById(buildingType.B27_Treasury)

        if (heroMasion) {
            this.heroFight.destroyAllChildren()
           
        } else {
            this.heroFight.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.heroFight.addChild(btnLock)
        }

        if (residence || palace) {
            this.palace.destroyAllChildren()
        } else {
            this.palace.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.palace.addChild(btnLock)
        }

        if (embassy) {
            this.union.destroyAllChildren()
        } else {
            this.union.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.union.addChild(btnLock)
        }

        if (academy) {
            this.develop.destroyAllChildren()
        } else {
            this.develop.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.develop.addChild(btnLock)
        }

        if (market) {
            this.transport.destroyAllChildren()
        } else {
            this.transport.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.transport.addChild(btnLock)
        }

        if (barracks) {
            this.train.destroyAllChildren()
        } else {
            this.train.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.train.addChild(btnLock)
        }

        if (treasury) {
            this.trea.destroyAllChildren()
        } else {
            this.trea.destroyAllChildren()
            let btnLock = instantiate(prefab)
            this.trea.addChild(btnLock)
        }

    }

    onAi() {
        //    UIManager.instance.tip('敬请期待')
        UIManager.instance.openUI(PrefabEnum.AIAsistant)
    }

    onMenu() {
        console.log('onMenu........')

        this.bg.active = !this.bg.active
        this.menuLayout.active = !this.menuLayout.active

        if (this.menuLayout.active) {
            this.menu.getComponent(Sprite).spriteFrame = this.menuImg2
        } else {
            this.menu.getComponent(Sprite).spriteFrame = this.menuImg1
        }

    }

    onBgClick() {
        this.bg.active = false
        this.menuLayout.active = false
    }

    async onBuild() {
        console.log('onBuild........')

        VillageController.instance.closeSpaceTip()
        let ui = await UIManager.instance.openUI(PrefabEnum.BuildingList)

    }
    onTrain() {
        let barracks = BManager.getBManageById(buildingType.B19_Barracks)
        if(!barracks){
            UIManager.instance.modal('提示','需要建造兵营，才能解锁训练功能', false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.TrainIndex)
    }
    onWorldBtn() {
        let name = director.getScene().name
        if (name == "world") {
            UIManager.instance.loadScene('village')
        } else {
            UIManager.instance.loadScene('world')
        }
    }
    onVill() {
        UIManager.instance.openUI(PrefabEnum.VillageResources)
    }
    onWorldDynamics() {
        // director.loadScene("worldDynamics");
        console.log('onWorldDynamics..........')
    }
    onVillage() {
        UIManager.instance.loadScene('village')
    }

    onUpGrade() {

    }

    onWar() {
        UIManager.instance.loadScene('tower')
    }
    onRank() {
        UIManager.instance.openUI(PrefabEnum.RankIndex)
    }
    onCopy() {
        UIManager.instance.openUI(PrefabEnum.CopyIndex)
    }
    onShop() {
        UIManager.instance.openUI(PrefabEnum.Shop)
    }
    onPagekage() {
        UIManager.instance.openUI(PrefabEnum.Rombaw)
    }



    onDevelop() {
        let academy = BManager.getBManageById(buildingType.B22_Academy)
        if(!academy){
            UIManager.instance.modal('提示','需要建造研究院，才能解锁研发功能', false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.DevelopIndex)
    }

    onTransport() {
        let market = BManager.getBManageById(buildingType.B17_Market)
        if(!market){
            UIManager.instance.modal('提示','需要建造市场，才能解锁运输功能', false)
            return
        }

        if(UserInfo.instance.villageData.villageObjList.length == 1){
            UIManager.instance.modal('提示',`您只有一个村庄，没有可运往的村庄。`, false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.Transport)
    }

    onClose() {
        UIManager.instance.closeUI(PrefabEnum.MenuLayout)
    }

    onArmy() {
        UIManager.instance.openUI(PrefabEnum.ArmyIndex)
    }

    onPalace() {
        let residence = BManager.getBManageById(buildingType.B25_Residence)
        let palace = BManager.getBManageById(buildingType.B26_Palace)
        if(!residence && !palace){
            UIManager.instance.modal('提示','需要建造大宫殿或者小宫殿，才能解锁宫殿功能', false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.PalaceIndex)
    }

    onReport() {
        UIManager.instance.openUI(PrefabEnum.ReportIndex)
    }

    onQuit() {
        location.href = 'https://920.cc/'
    }

    onArt() {
        UIManager.instance.tip('敬请期待')

    }

    onHeroFight() {

        let building =  BManager.getBManageById(buildingType.B37_HeroMasion)
        if (!building) {
            UIManager.instance.modal('提示','需要建造英雄别院，才能解锁英雄功能', false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.HeroYard)
    }
    onHeroYar() {

        UIManager.instance.openUI(PrefabEnum.HeroYard)
    }

    onUnion() {
        let embassy = BManager.getBManageById(buildingType.B18_Embassy)
        if(!embassy){
            UIManager.instance.modal('提示','需要建造使节馆，才能解锁联盟功能', false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.AllyIndex)
    }
    onPass() {
        UIManager.instance.tip('敬请期待')
    }
    onTrea() {
        let treasury = BManager.getBManageById(buildingType.B27_Treasury)
        if(!treasury){
            UIManager.instance.modal('提示','需要建造宝库，才能解锁宝库功能', false)
            return
        }
        UIManager.instance.openUI(PrefabEnum.Trea)
    }
}


